<?php

namespace app\api\controller;

use app\common\controller\Api;
use app\api\service\DemoService;
use fast\Random;
use think\Hook;

/**
 * 示例接口
 */
class Demo extends Api
{

    //如果$noNeedLogin为空表示所有接口都需要登录才能请求
    //如果$noNeedRight为空表示所有接口都需要验证权限才能请求
    //如果接口已经设置无需登录,那也就无需鉴权了
    //
    // 无需登录的接口,*表示全部
    protected $noNeedLogin = ['*'];
    // 无需鉴权的接口,*表示全部
    protected $noNeedRight = ['*'];

    public function _initialize()
    {
        parent::_initialize();
    }

    /**
     * 发放优惠卷
     *
     */
    public function issue_coupon()
    {
        $data = $this->request->post();
        $res = DemoService::issueCoupon($data);
        return json($res);
    }

    public function test()
    {
        //发送后台新订单通知

//            $push_api_url = request()->domain().":2121/";
//
//            $content = '测试'.Random::alnum(4);
//            $this->model->create([
//                'title'         =>  $content,
//                'content'       =>  $content,
//                'uid'           =>  1,
//                'role'          =>  1 //后台推送
//            ]);
//
//
//            $post_data = array(
//                'type' => 'publish',
//                'content' => $content,
//                'to' => '',
//            );
//            $ch = curl_init ();
//            curl_setopt ( $ch, CURLOPT_URL, $push_api_url );
//            curl_setopt ( $ch, CURLOPT_POST, 1 );
//            curl_setopt ( $ch, CURLOPT_HEADER, 0 );
//            curl_setopt ( $ch, CURLOPT_RETURNTRANSFER, 1 );
//            curl_setopt ( $ch, CURLOPT_POSTFIELDS, $post_data );
//            curl_setopt ( $ch, CURLOPT_HTTPHEADER, array("Expect:"));
//            $return = curl_exec ( $ch );
//            curl_close ( $ch );

        $noticeParams = [
            'event' => 'user_order',
            'params' => array (
                'receiver_admin_ids' => '',
                'receiver_admin_group_ids' => '',
                'order_id' => '35',
                'mobile' => '18838043905',
                'type' => '维修',
                'order_no' => '202501087575369691',
            )];
        \Think\Hook::listen('send_notice', $noticeParams);
//    var_export($return);

    }
}
